package com.yc.d0422;

import com.yc.util.DBHelper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import static java.lang.Thread.sleep;

public class test {
    public static void main(String[] args) throws IOException {
        // downHtml("https://www.jiushujie.com/user/27025276/sell");
//        captureJob("https://www.jiushujie.com/sell/455239495");
        captureJobs();
    }

    public static String fileDir = "D:/study/z/旧书街/";

    public static void downHtml(String address) throws IOException {
        URL url = new URL(address);
        String filename = url.getFile();
        String filepath = fileDir + filename;
        download(address, filepath);
        /**
         * 解析页面资源：
         * css=><link href="....">,
         * js => <script src="....",
         * 图片 =><img src=".....">
         */

        final Document doc = Jsoup.parse(new File(filepath), "utf-8");
//        doc.select() ==> 相当于js的document.querySelectorAll();
//        doc.selectFirst() ==> 相当于js的 document.querySelector();
        Elements css = doc.select("link[rel=stylesheet]");
        css.forEach(element -> {
            //获取属性值
            String href = element.attr("href");
            href = Ispath(href);
//            //查找最后一个  s      https://www.jiushujie.com/sell/425923509  取到s前
//            int i = address.lastIndexOf("s");
//            String webpath = address.substring(0, i) + href;
            String webpath = Iswebpath(address,href);
//            String diskpath = fileDir + href;
            String diskpath = diskPath(href);
            try {
                download(webpath, diskpath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        Elements js = doc.select("script[src]");
        js.forEach(element -> {
            //获取属性值
            String src = element.attr("src");

//            //查找最后一个
//            int i = address.lastIndexOf("/");
//            String webpath = address.substring(0, i + 1) + src;
            src = Ispath(src);
            String webpath = Iswebpath(address,src);
            String diskpath = diskPath(src);
            try {
                download(webpath, diskpath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        Elements img = doc.select("img[src]");
        img.forEach(element -> {
            //获取属性值
            String src = element.attr("src");
//            //查找最后一个
//            int i = address.lastIndexOf("/");
//            String webpath = address.substring(0, i + 1) + src;
            src = Ispath(src);
            String webpath = Iswebpath(address,src);
            String diskpath = diskPath(src);
            try {
                download(webpath, diskpath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

    }


    public static void download(String address, String filepath) throws IOException {
        System.out.println("开始下载文件");
        System.out.println("address = " + address);
        System.out.println("filepath = " + filepath);

        File file = new File(filepath);
        //获取文件所在文件夹
        final File parentFile = file.getParentFile();
        if (parentFile.exists() == false) {
            //创建文件夹啊
            parentFile.mkdirs();
        }

        URL url = new URL(address);
        URLConnection conn = url.openConnection();

        try (final InputStream in = conn.getInputStream();
             FileOutputStream fos = new FileOutputStream(filepath)) {
            byte[] bytes = new byte[1024];
            int count;
            while ((count = in.read(bytes)) > -1) {
                //输出到文件
                fos.write(bytes, 0, count);
            }
        }
        System.out.println("下载文件结束");
    }

    public static String Ispath(String src){
        //找?
        if (src.contains("?")){
            int j = src.indexOf("?");
            src = src.substring(0,j);
        }
        String s = src.substring(0, 2);
        String ss = src.substring(0,1);
        if ("/".equals(ss)){
            if ("//".equals(s)){
                return src;
            }
            src = src.substring(1);
            return src;
        }else {
            return src;
        }
    }

    public static String Iswebpath(String address,String href) {
        if ("//".equals(href.substring(0,2))){
            href = "http:" + href;
            return href;
        }else if ("ht".equals(href.substring(0,2))){
            return href;
        } else {
            //查找最后一个  s      https://www.jiushujie.com/sell/425923509  取到s前
            int i = address.lastIndexOf("s");
            String webpath = address.substring(0, i) + href;
            return webpath;
        }
    }

    public static String diskPath (String href) {
//        String diskpath = fileDir + src;
        //libs.baidu.com/jqueryui/1.8.22/jquery-ui.min.js
        // /themes/classic/assets/css/form.css?ver=4
        String diskpath = fileDir + href;
        if (href.contains("ht") || (href.contains("p:")) || (href.contains("//"))){
            if (href.contains("me")){
                int i = href.indexOf("me");
                if (i < 20 ){
                    href = href.substring(i + 3);
                }
            }
            if (href.contains("com")){
                int i = href.indexOf("com");
                href = href.substring(i + 4);
            }
            if(href.contains("!")){
                int k = href.lastIndexOf("!");
                href = href.substring(0,k);
            }
            diskpath = fileDir + href;
            return diskpath;
        }else {
            return diskpath;
        }
    }



    public static void captureJobs() throws IOException {
        for (int i = 1; i <= 20 ; i++) {
            String address = "https://www.jiushujie.com/sell?sort=created_at.desc&tag=recommend&page=" + i;
            Document doc = Jsoup.parse(new URL(address), 5000);
            Elements elements = doc.select(".book_item");
            elements.forEach(element ->{
                Element a = element.selectFirst("a");
                String href = a.attr("href");
                System.out.println(href);
//                String webpath = "https://www.jiushujie.com" + href;
                String webpath = href;
                try {
                    captureJob(webpath);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }
    }

    public static void captureJob(String address) throws IOException {
//        address = "https://www.jiushujie.com/sell/455239483";

        Document doc = Jsoup.parse(new URL(address), 5000);

        String id = address.replaceAll(".*/(\\d+)$", "$1");
        System.out.println(id);
        //text() ==> innerText


        //定位数书籍信息
        Elements elements = doc.select(".info");
        //书籍简介
        Elements element = doc.select(".book_introduction_container");
        //个人信息
        Elements people_element = doc.select(".user_nav_info");
        //认证信息
        Elements authentication_information = doc.select(".self_introduction");

        String people_text = people_element.text();
        String[] information = people_text.split(" ");
        //昵称
        String nickname = information[0];
        //性别
        String sex = information[1];
        //大学
        String university = information[2];
        //上次登陆
        String last_login = information[4];
        //关注
        String followed = information[5].replaceAll("\\D+","");
        //粉丝
        String fan = information[6].replaceAll("\\D+","");


        String AI_text = authentication_information.text();
        String[] AI_information = AI_text.split(" ");
        //姓名
        String name = AI_information[3];
        //手机号
        String phone = AI_information[5];
        //地址
        String addr = AI_information[7];

        String text = elements.get(0).text();
        String[] arr  =  text.split(" ");
        //书名
        String book_name = arr[0];
        //出版社
        String press = arr[1];
        //作者
        String author = arr[3];
        //ISBN
        String ISBN = arr[4];
        //现价
        String current_price = arr[5].substring(1);
        //原价
        String price = arr[6].substring(1);
        //成色
        String fineness = arr[7];
        //分类
        String type = arr[8];
        //简介
        String synopsis = element.text();

        System.out.println(synopsis);


        try {
            String sql = "insert into user values(?,?,?,?,?,?,?,?,?)";
            DBHelper.update(sql, nickname, sex, university, last_login, followed, fan, name, phone, addr);
            System.out.println("存入用户信息成功");
        }catch (Exception e) {
            //            e.printStackTrace();
            System.out.println("昵称:" + nickname + " " + "姓名:" + name + " 的用户已存在,存入失败");
        }

        try {
            String sql = "insert into jsj_book values(?,?,?,?,?,?,?,?,?,?)";
            DBHelper.update(sql, id, book_name, press, author, ISBN, current_price, price, fineness, type,synopsis);
            System.out.println("存入书籍信息成功");
        }catch (Exception e){
//            e.printStackTrace();
            System.out.println("书名:" + book_name + " " + "id:" + id + " 的书已存在,存入失败");
        }

    }


}
